from django.db import models
from user_app.models import User
from vehicle_app.models import Vehicle


class Review(models.Model):
    """资质审核模型"""
    
    class Type(models.TextChoices):
        USER = 'user', '用户资质审核'
        VEHICLE = 'vehicle', '车辆资质审核'
    
    class Status(models.TextChoices):
        PENDING = 'pending', '待审核'
        APPROVED = 'approved', '已通过'
        REJECTED = 'rejected', '已拒绝'
    
    # 审核类型和状态
    type = models.CharField(max_length=20, choices=Type.choices, verbose_name='审核类型')
    status = models.CharField(max_length=20, choices=Status.choices, default=Status.PENDING, verbose_name='审核状态')
    
    # 关联对象
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, verbose_name='用户')
    vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE, null=True, blank=True, verbose_name='车辆')
    
    # 审核信息
    reviewer = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='reviews', verbose_name='审核员')
    reason = models.TextField(blank=True, verbose_name='审核说明')
    
    # 时间戳
    submitted_at = models.DateTimeField(auto_now_add=True, verbose_name='提交时间')
    reviewed_at = models.DateTimeField(null=True, blank=True, verbose_name='审核时间')
    
    class Meta:
        verbose_name = '资质审核'
        verbose_name_plural = '资质审核'
        ordering = ['-submitted_at']
    
    def __str__(self):
        if self.type == self.Type.USER:
            return f"用户资质审核 - {self.user.phone if self.user else '未知用户'}"
        elif self.type == self.Type.VEHICLE:
            return f"车辆资质审核 - {self.vehicle.license_plate if self.vehicle else '未知车辆'}"
        return f"资质审核 - {self.get_type_display()}"